<template>
	<div>
		<HomeHeader></HomeHeader>
		<HomeLeft :cur="3"></HomeLeft>
		<div id="main">
			<div id="web_con" v-if="isedit">
				<div class="web_wp">
					<div class="web_location">
						<div class="here_area">
							<strong>当前位置：</strong>财富管理&nbsp;&nbsp;»&nbsp;&nbsp;编辑
						</div>
						<div class="here_btn">
							<a href="javascript:history.back()" class="web_btn btn_add"><em
									class="iconfont icon-shdcfanhui"></em>返回</a>
						</div>
					</div>

					<div class="tabTitle">
						<ul class="clearfix">
							<li class="cur"><a href="javascript:;">基本信息</a></li>
						</ul>
						<div class="tabCont">
							<div class="cont">
								<table class="dataTable">
									<tr>
										<td class="ltd">交易编号</td>
										<td class="rtd">
											<input type="text" class="input-text" disabled :value="info.payno">
										</td>
									</tr>
									<tr>
										<td class="ltd">用户名称</td>
										<td class="rtd">
											<input type="text" class="input-text" disabled :value="info.user.nickname">
										</td>
									</tr>


									<tr>
										<td class="ltd">存入金额</td>
										<td class="rtd">
											<input type="text" class="input-text" disabled :value="info.pay_fee">
										</td>
									</tr>
									<tr>
										<td class="ltd">年化收益率</td>
										<td class="rtd">
											<input type="text" class="input-text" disabled
												:value="info.current.balance/info.pay_fee*100 +'%'">
										</td>
									</tr>
									<tr>
										<td class="ltd">预计收益</td>
										<td class="rtd">
											<input type="text" class="input-text" disabled
												:value="info.current.balance">
										</td>
									</tr>
									<tr>
										<td class="ltd">赠送积分</td>
										<td class="rtd">
											<input type="text" class="input-text" disabled :value="info.pay_fee">
										</td>
									</tr>

									<tr v-if="info.status==30">
										<td class="ltd">生效日期</td>
										<td class="rtd">
											<!-- 	<input type="text" class="input-text" :value="info.current.start_at"> -->

											<el-date-picker style="width: 300px;" v-model="start_at" type="date"
												placeholder="选择日期" :editable="false" @change="StartAt"
												value-format="yyyy-MM-dd">
											</el-date-picker>

											<el-button style="margin-left: 15px;" type="primary" @click="EditTime()">保存生效日期</el-button>
										</td>
									</tr>

									<tr v-if="info.status==30">
										<td class="ltd">到期日期</td>
										<td class="rtd">
											<input type="text" class="input-text" :value="end_at">
										</td>
									<tr v-if="info.status==30||info.status==20">
										<td class="ltd">支付凭证</td>
										<td class="rtd">
											<img style="width: 200px;" :src="info.credentials" alt="">
										</td>
									</tr>
									<tr>
										<td class="ltd">状态</td>
										<td class="rtd">
											<input v-if="info.status==10" disabled type="text" class="input-text"
												value="线下待付款">
											<input v-if="info.status==20" disabled type="text" class="input-text"
												value="待确认">
											<input disabled v-if="info.status==30&&info.current.action=='renew'"
												type="text" class="input-text" value="进行中(到期续签)">
											<input disabled v-if="info.status==30&&info.current.action=='redeem'"
												type="text" class="input-text" value="进行中(到期赎回)">
											<input disabled v-if="info.status==40" type="text" class="input-text"
												value="已赎回">
										</td>
									</tr>
								</table>
							</div>
						</div>
						<!-- <div>
							<div class="contBtn">
								<table class="dataTable">
									<tfoot>
										<tr>
											<td class="ltd"></td>
											<td class="rtd">
												<a href="javascript:;" class="web_btn btn_add" @click="SubMit()">
													<em class="iconfont icon-shdcbaocun"></em>保存
												</a>
											</td>
										</tr>
									</tfoot>
								</table>
							</div>
						</div> -->
					</div>
				</div>

			</div>
		</div>
	</div>
</template>

<script>
	import HomeHeader from '../components/Header.vue';
	import HomeLeft from '../components/Left.vue';
	import PicUp from '../components/PicUp.vue'
	export default {
		components: {
			HomeHeader,
			HomeLeft,
			PicUp
		},
		data() {
			return {
				info: [],
				isedit: false,
				id: 0,
				start_at: '',
				end_at: '',
			}
		},
		onLoad(e) {
			let that = this;

			if (e.id) {
				that.id = e.id;
				that.GetDeta(e.id);
			}
		},
		methods: {
			
			EditTime:function(){
				let that = this;
				if(that.start_at == ''){
					uni.showToast({
						icon: 'none',
						title: '请选择生效日期',
						duration: 2000
					});
				}
				else{
					let _data = {
						start_at: that.start_at,
						end_at:that.end_at
					};
					that.common.http('PUT', 'history/' + that.id , _data, function(res) {
						uni.hideLoading();
						console.log(res)
						if (res.code == 200) {
							uni.showToast({
								icon: 'none',
								title: '修改成功！',
								duration: 2000
							});
							setTimeout(function() {
								that.navigateBack();
							}, 1500);
						}
					});
				}
			},
			

			StartAt: function(e) {
				this.end_at = this.calculatePreviousYearDate(e);
			},


			calculatePreviousYearDate(dateString) {
				const date = new Date(dateString);
				const nextYear = new Date(date.getFullYear() + 1, date.getMonth(), date.getDate());
				const nextYearMinusOneDay = new Date(nextYear.getFullYear(), nextYear.getMonth(), nextYear.getDate() - 1);

				const year = nextYearMinusOneDay.getFullYear();
				const month = String(nextYearMinusOneDay.getMonth() + 1).padStart(2, '0');
				const day = String(nextYearMinusOneDay.getDate()).padStart(2, '0');

				const formattedDate = `${year}-${month}-${day}`;

				return formattedDate;
			},


			GetDeta: function(id) {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				that.common.http('GET', 'pay/' + id, '', function(res) {
					uni.hideLoading();
					console.log(res.data)
					that.info = res.data;
					that.start_at = res.data.current.start_at;
					that.end_at = res.data.current.end_at;

					that.isedit = true;
				});
			},
			navigateBack() {
				// #ifdef H5
				const pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack(1)
					return;
				}
				//使用vue-router返回上一级
				let a = this.$router.go(-1)
				if (a == undefined) {}
				return;
				// #endif
				uni.navigateBack();
			},

		}
	}
</script>

<style>


</style>